import re

import requests
from bs4 import BeautifulSoup


class Web:
    def __init__(self):
        self.host = "https://g8pc786d.yichafen.com"
        self.check_url = "https://g8pc786d.yichafen.com/public/queryresult/from_device/mobile.html"

  
    def getScore(self, url, ids):
        session = requests.Session()

        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0",
            "Referer": url,
        }

        res = session.get(headers=headers, url=url)
        soup = BeautifulSoup(res.text, "html.parser")
        project = {}
        project["name"] = soup.find("div", attrs={"class": "s_page-header_color"}).string.strip()
        project['url'] = url
        print(project)

        scores = []
        post_url = re.findall(r"/public/verifycondition/sqcode.*from_device/mobile.html", res.text)[0]
        for id in ids:
            values = {"s_kaohao": id["xuehao"], "s_xingming": id["name"]}

            res2 = session.post(headers=headers, url=self.host + post_url, data=values)

            respone = session.get(headers=headers, url=self.check_url).text

            soup = BeautifulSoup(respone, "html.parser")

            all_tds_left = soup.find_all("td", attrs={"class": "left_cell"})

            all_tds_right = soup.find_all("td", attrs={"class": "right_cell"})

            result = {}
            for i in range(len(all_tds_left)):
                result[all_tds_left[i].string] = all_tds_right[i].string

            # print(result)

            scores.append(result)
        return project, scores


# https://g8pc786d.yichafen.com/qz/1680SzahZt